home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / shareware / intuition / yak_1.57 / source / convert.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  5KB  |  221 lines

  1. /****************************************************************************
  2.  * Convert Yak 1.4 settings files to Yak 1.5 settings files.
  3.  *
  4.  * Read 1.4 yak.prefs, write 1.5 yak.prefs and yak.hotkeys,
  5.  * rename old yak.prefs as yak.prefs.old.
  6.  * 
  7.  * Martin W Scott, 14 May 1993.
  8.  ****************************************************************************/
  9. #include <exec/types.h>
  10. #include <dos/dos.h>
  11. #include <intuition/intuition.h>
  12. #include <proto/exec.h>
  13. #include <proto/dos.h>
  14. #include <proto/intuition.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17.  
  18. #include "yak.h"
  19. #include "hotkey_types.h"
  20.  
  21. struct IntuitionBase *IntuitionBase;
  22.  
  23. static void
  24. CloseResources(void)
  25. {
  26.     if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  27. }
  28.  
  29. static BOOL
  30. OpenResources(void)
  31. {
  32.     if (IntuitionBase = (void *)OpenLibrary("intuition.library", 37L))
  33.         return TRUE;
  34.     return FALSE;
  35. }
  36.  
  37. /* simple requester with args */
  38. void
  39. PostError(char *body, ... )
  40. {
  41.     struct EasyStruct es;
  42.     va_list args;
  43.  
  44.     if (!IntuitionBase)
  45.     {
  46.         Write(Output(), "Need AmigaDos 2.0+\n", -1);
  47.         return;
  48.     }
  49.  
  50.     /* setup the argument array */
  51.     va_start( args, body );
  52.  
  53.     /* initialise the structure */
  54.     es.es_StructSize = sizeof(struct EasyStruct);
  55.     es.es_Flags = 0L;
  56.     es.es_Title = "Yak Message";
  57.     es.es_TextFormat = body;
  58.     es.es_GadgetFormat = "OK";
  59.  
  60.     /* display the requester */
  61.     EasyRequestArgs(NULL, &es, NULL, args);
  62.  
  63.     /* free the arguments */
  64.     va_end( args );
  65. }
  66.  
  67. /* write a LONG to a file (in binary format) - returns success*/
  68. static BOOL __regargs
  69. FWriteLong(BPTR fh, LONG n)
  70. {
  71.     return (BOOL)(FWrite(fh, (UBYTE *)&n, sizeof(LONG), 1) == 1);
  72. }
  73.  
  74. /* read a LONG to a file (in binary format) - returns success */
  75. static BOOL __regargs
  76. FReadLong(BPTR fh, LONG *n)
  77. {
  78.     return (BOOL)(FRead(fh, (UBYTE *)n, sizeof(LONG), 1) == 1);
  79. }
  80.  
  81. /* write a string to a file (in binary format) - returns success*/
  82. /* '\n' is appended */
  83. static BOOL __regargs
  84. FWriteString(BPTR fh, char *buf)
  85. {
  86.     FPuts(fh, buf);
  87.     FPutC(fh, '\n');
  88.     return (BOOL)(IoErr() == 0);
  89. }
  90.  
  91. /* read a string to a file (in binary format) - returns success */
  92. /* '\n' is stripped and buf null-terminated; assumes dest large enough */
  93. static BOOL __regargs
  94. FReadString(BPTR fh, char *buf, LONG len)
  95. {
  96.     FGets(fh, buf, len-1);
  97.     buf[strlen(buf)-1] = '\0';    /* '\n' --> '\0' */
  98.     return (BOOL)(IoErr() == 0);
  99. }
  100.  
  101. #define OLD_CONFIG_ID    0x594b3133    /* YK13 */
  102. #define NEW_CONFIG_ID    0x594b3135    /* YK15 */
  103. #define HOTKEY_ID    0x594B4B31    /* 'YKK1' */
  104.  
  105. UWORD convtab[] = {
  106.     SHOW_INTERFACE, CLOSE_WINDOW, ZIP_WINDOW, SHRINK_WINDOW,
  107.     EXPAND_WINDOW, ACTIVATE_WORKBENCH, OPEN_PALETTE, DOS_COMMAND,
  108.     INSERT_DATE, CYCLE_WINDOWS, SCREEN_TO_FRONT, CENTRE_SCREEN,
  109.     SCREEN_TO_BACK, BLANK_DISPLAY
  110. };
  111.  
  112. void
  113. DoConvert(void)
  114. {
  115.     BPTR inprefs, outprefs, outkeys;
  116.     LONG n, i;
  117.     UWORD type, opts;
  118.     BOOL boolboy;
  119.     char buf[256];
  120.  
  121.     if (inprefs = Open("S:Yak.prefs", MODE_OLDFILE))
  122.     {
  123.       if (outprefs = Open("S:Yak.prefs.new", MODE_NEWFILE))
  124.       {
  125.         if (outkeys = Open("S:Yak.hotkeys", MODE_NEWFILE))
  126.         {
  127.           FReadLong(inprefs, &n);
  128.           if (n == OLD_CONFIG_ID)
  129.           {
  130.             FWriteLong(outprefs, NEW_CONFIG_ID); 
  131.             FWriteLong(outkeys, HOTKEY_ID);
  132.  
  133.         /* TOGGLES */
  134.         FReadLong(inprefs, &n);
  135.         FWriteLong(outprefs, n);
  136.         for (i = 0; i < n; i++)
  137.         {
  138.           FRead(inprefs, (UBYTE *)&boolboy, sizeof(BOOL), 1);
  139.           FWrite(outprefs, (UBYTE *)&boolboy, sizeof(BOOL), 1);
  140.         }
  141.  
  142.         /* HOTKEYS */
  143.         FReadLong(inprefs, &n);
  144.         FWriteLong(outkeys, n);
  145.         for (i = 0; i < n; i++)
  146.         {
  147.           FReadString(inprefs, buf, 256);
  148.           type = convtab[i]; opts = 0;
  149.           FWrite(outkeys, (UBYTE *)&type, sizeof(UWORD), 1);
  150.           FWrite(outkeys, (UBYTE *)&opts, sizeof(UWORD), 1);
  151.           FWriteString(outkeys, buf);
  152.           FWriteString(outkeys, "");
  153.         }
  154.         
  155.         /* PATTERNS */
  156.         FReadLong(inprefs, &n);
  157.         FWriteLong(outprefs, n);
  158.         for (i = 0; i < n; i++)
  159.         {
  160.           FReadString(inprefs, buf, 256);
  161.           FWriteString(outprefs, buf);
  162.         }
  163.  
  164.         /* POPKEY */
  165.         FReadString(inprefs, buf, 256);
  166.  
  167.         /* DATE FORMAT */
  168.         FReadString(inprefs, buf, 256);
  169.  
  170.         /* CLICK VOLUME */
  171.         FReadLong(inprefs, &n);
  172.         FWriteLong(outprefs, n);
  173.  
  174.         /* BLANK SECS */
  175.         FReadLong(inprefs, &n);
  176.         FWriteLong(outprefs, n);
  177.  
  178.         /* MBLANK SECS */
  179.         if (!FReadLong(inprefs, &n))
  180.             n = 5;
  181.         FWriteLong(outprefs, n);
  182.  
  183.         /* MBLANK METHOD */
  184.         if (!FReadLong(inprefs, &n))
  185.             n = MB_SPRITES;
  186.         FWriteLong(outprefs, n);
  187.  
  188.         Close(inprefs);
  189.         Close(outprefs);
  190.         Close(outkeys);
  191.  
  192.         Rename("S:Yak.prefs", "S:Yak.prefs.old");
  193.         Rename("S:Yak.prefs.new", "S:Yak.prefs");
  194.         
  195.         PostError("Wrote new Yak.prefs file and Yak.hotkeys file.\nOld preferences file is S:Yak.prefs.old.");
  196.         return;
  197.           }
  198.           PostError("Invalid existing Yak.prefs file");
  199.           Close(outkeys);
  200.         }
  201.          else PostError("Can't create Yak.hotkeys file");
  202.         Close(outprefs);
  203.       }
  204.       else PostError("Can't create Yak.prefs.new file");
  205.       Close(inprefs);
  206.     }
  207.     else PostError("Can't open existing Yak.prefs file");
  208. }
  209.  
  210. void
  211. __main(void)
  212. {
  213.     if (OpenResources())
  214.     {
  215.         DoConvert();
  216.         CloseResources();
  217.     }
  218.  
  219. } /* main */
  220.  
  221.